package com.itheima.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.domain.pojo.Student;
import com.itheima.domain.query.QueryParam;
import com.itheima.domain.vo.PageResult;
import com.itheima.exception.StudentNoPhoneIdCardException;
import com.itheima.mapper.StudentMapper;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author:篍
 * @Date: 2025/07/14 19:25
 * @Description:
 */
@Service
@Slf4j
public class StudentServiceImpl implements StudentService {
    @Autowired
    StudentMapper studentMapper;

    /**
     * 根据id查询学生
     * @param id
     * @return
     */
    @Override
    public Student findStudentById(Integer id) {
        return studentMapper.findStudentById(id);
    }

    /**
     * 用pagehelper 根据页码和每页条数分页查询学员
     * @param queryParam
     * @return
     */
    @Override
    public PageResult<Student> pageListStudents(QueryParam queryParam) {
        Integer page=queryParam.getPage();
        Integer pageSize=queryParam.getPageSize();
        PageHelper.startPage(page,pageSize);
        Page<Student> studentPage= studentMapper.findAllStudents(queryParam);
        return new PageResult<>(studentPage.getTotal(),studentPage.getResult());
    }

    /**
     * 添加学生
     * @param student
     */
    @Override
    public void addStudent(Student student) {
        //补全创建时间和修改时间
        LocalDateTime now = LocalDateTime.now();
        student.setCreateTime(now);
        student.setUpdateTime(now);
        log.warn("student={}",student);
        //主键回填
        studentMapper.addStudent(student);
    }

    /**
     * 修改学生
     * @param student
     */
    @Override
    public void updateStudent(Student student) {
        Student dbStudent = studentMapper.findStudentById(student.getId());
        //判断学号手机号身份证号变没有
        String dbStudentNo = dbStudent.getNo();
        String no = student.getNo();
        String dbStudentPhone = dbStudent.getPhone();
        String phone = student.getPhone();
        String dbStudentCard = dbStudent.getIdCard();
        String idCard = student.getIdCard();

        if (!StrUtil.equals(dbStudentNo,no)){
            //判断学号是否重复
            Long count=studentMapper.findNo(no);
            if(count>0){
                throw new StudentNoPhoneIdCardException("学号"+no+"与他人重复，请修改");
            }
        }
        if (!StrUtil.equals(dbStudentPhone,phone)){
            //判断手机号是否重复
            Long count=studentMapper.findPhone(phone);
            if (count>0){
                throw new StudentNoPhoneIdCardException("手机号"+phone+"与他人重复，请修改");
            }
        }
        if (!StrUtil.equals(dbStudentCard,idCard)){
            //判断身份证号是否重复
            Long count=studentMapper.findIdCard(idCard);
            if (count>0){
                throw new StudentNoPhoneIdCardException("身份证号"+idCard+"与他人重复，请修改");
            }
        }
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.updateStudent(student);
    }

    /**
     * 根据id数组删除学员
     * @param ids
     */
    @Override
    public void deleteStudent(Integer[] ids) {
        if(ArrayUtil.isNotEmpty(ids)){
            studentMapper.deleteStudent(ids);
        }
    }

    /**
     * 根据id计算违纪扣分分数
     * @param id
     * @param score
     */
    @Override
    public void violationScore(Integer id, Short score) {
        studentMapper.violationScore(id,score);
    }


//    /**
//     * 封装判重代码
//     */
//    public  void repeat(Student student){
//        Student dbStudent = studentMapper.findStudentById(student.getId());
//        //判断学号手机号身份证号变没有
//        String dbStudentNo = dbStudent.getNo();
//        String no = student.getNo();
//        String dbStudentPhone = dbStudent.getPhone();
//        String phone = student.getPhone();
//        String dbStudentCard = dbStudent.getIdCard();
//        String idCard = student.getIdCard();
//    }
}
